using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Matrix.ServiceDiscovery.Dtos;
using Matrix.ServiceDiscovery.Enums;

namespace Matrix.ServiceDiscovery.Services
{
    /// <summary>
    /// 服务发现应用服务接口
    /// 自动暴露为HTTP API接口
    /// </summary>
    public interface IServiceDiscoveryAppService : ICrudAppService<
        ServiceInfoDto,
        Guid,
        GetServiceListInput,
        CreateServiceInfoDto,
        UpdateServiceInfoDto>
    {
        #region 查询操作

        /// <summary>
        /// 根据服务名称获取服务信息
        /// </summary>
        /// <param name="serviceName">服务名称</param>
        /// <returns>服务信息</returns>
        Task<ServiceInfoDto> GetByNameAsync(string serviceName);

        /// <summary>
        /// 获取服务的所有实例
        /// </summary>
        /// <param name="serviceId">服务ID</param>
        /// <returns>实例列表</returns>
        Task<ListResultDto<ServiceInstanceDto>> GetInstancesAsync(Guid serviceId);

        /// <summary>
        /// 获取健康实例
        /// </summary>
        /// <param name="serviceName">服务名称</param>
        /// <returns>健康实例列表</returns>
        Task<ListResultDto<ServiceInstanceDto>> GetHealthyInstancesAsync(string serviceName);

        /// <summary>
        /// 获取负载均衡配置
        /// </summary>
        /// <param name="serviceId">服务ID</param>
        /// <returns>负载均衡配置</returns>
        Task<LoadBalancingConfigDto> GetLoadBalancingConfigAsync(Guid serviceId);

        /// <summary>
        /// 获取健康检查记录
        /// </summary>
        /// <param name="serviceInstanceId">服务实例ID</param>
        /// <param name="input">查询参数</param>
        /// <returns>健康检查记录</returns>
        Task<PagedResultDto<HealthCheckRecordDto>> GetHealthCheckRecordsAsync(
            Guid serviceInstanceId,
            GetHealthCheckRecordsInput input);

        /// <summary>
        /// 搜索服务
        /// </summary>
        /// <param name="input">搜索参数</param>
        /// <returns>搜索结果</returns>
        Task<PagedResultDto<ServiceInfoDto>> SearchServicesAsync(SearchServicesInput input);

        /// <summary>
        /// 获取服务统计信息
        /// </summary>
        /// <returns>统计信息</returns>
        Task<ServiceStatisticsDto> GetServiceStatisticsAsync();

        #endregion

        #region 管理操作

        /// <summary>
        /// 激活服务
        /// </summary>
        /// <param name="id">服务ID</param>
        /// <returns>服务信息</returns>
        Task<ServiceInfoDto> ActivateAsync(Guid id);

        /// <summary>
        /// 停用服务
        /// </summary>
        /// <param name="id">服务ID</param>
        /// <returns>服务信息</returns>
        Task<ServiceInfoDto> DeactivateAsync(Guid id);

        /// <summary>
        /// 注册服务实例
        /// </summary>
        /// <param name="input">注册参数</param>
        /// <returns>实例信息</returns>
        Task<ServiceInstanceDto> RegisterInstanceAsync(RegisterServiceInstanceDto input);

        /// <summary>
        /// 注销服务实例
        /// </summary>
        /// <param name="serviceId">服务ID</param>
        /// <param name="instanceId">实例ID</param>
        Task UnregisterInstanceAsync(Guid serviceId, string instanceId);

        /// <summary>
        /// 更新实例健康状态
        /// </summary>
        /// <param name="instanceId">实例ID</param>
        /// <param name="status">健康状态</param>
        Task UpdateInstanceHealthStatusAsync(Guid instanceId, HealthStatus status);

        /// <summary>
        /// 记录实例心跳
        /// </summary>
        /// <param name="instanceId">实例ID</param>
        Task RecordInstanceHeartbeatAsync(Guid instanceId);

        /// <summary>
        /// 更新负载均衡配置
        /// </summary>
        /// <param name="serviceId">服务ID</param>
        /// <param name="input">配置参数</param>
        /// <returns>负载均衡配置</returns>
        Task<LoadBalancingConfigDto> UpdateLoadBalancingConfigAsync(
            Guid serviceId,
            UpdateLoadBalancingConfigDto input);

        #endregion

        #region 扩展操作

        /// <summary>
        /// 批量获取服务信息
        /// </summary>
        /// <param name="serviceNames">服务名称列表</param>
        /// <returns>服务信息字典</returns>
        Task<Dictionary<string, ServiceInfoDto>> GetBatchAsync(List<string> serviceNames);

        /// <summary>
        /// 批量更新健康状态
        /// </summary>
        /// <param name="inputs">健康状态更新列表</param>
        Task BatchUpdateHealthStatusAsync(List<UpdateHealthStatusInput> inputs);

        #endregion
    }

    /// <summary>
    /// 健康状态更新输入
    /// </summary>
    public class UpdateHealthStatusInput
    {
        /// <summary>
        /// 实例ID
        /// </summary>
        public Guid InstanceId { get; set; }

        /// <summary>
        /// 健康状态
        /// </summary>
        public HealthStatus Status { get; set; }
    }
}